package cn.jdy.framework.db.redis;

import java.util.LinkedList;
import java.util.List;
import java.util.ResourceBundle;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedisPool;

import cn.jdy.framework.util.PropertyUtil;

/**
 *   reids 数据库连接池管理
 * @description: reids 数据库连接池管理
 * @author  WuZhiliang
 * @date  2013-07-03 10:10
 */
public class JedisPoolManager {
	
  /** redis jedis 配置信息 **/
  public static final String CONFIG = "config";// 配置文件名
  public static final String MAXACTIVE = "redis.pool.maxActive";// 最大分配的对象数
  public static final String MAXIDLE = "redis.pool.maxIdle"; // 最大能够保持idel状态的对象数
  public static final String MAXWAIT = "redis.pool.maxWait"; // 等待时间
  public static final String TESTONBORROW = "redis.pool.testOnBorrow"; // 当调用borrow
                                                                       // Object方法时，是否进行有效性检查
  public static final String TESTONRETURN = "redis.pool.testOnReturn"; // #当调用return
                                                                       // Object方法时，是否进行有效性检查
  public static final String IP = "redis.ip"; // reids IP地址
  public static final String PORT = "redis.port"; // #reids Port
  
	public static  JedisPool jedisPool; // 数据库连接池
	private  ShardedJedisPool shardedJedisPool;	
	private JedisPoolManager(){	
	}
	
	/**
	 *   获取数据库连接池
	 * @description: 获取数据库连接池
	 * @author  WuZhiliang
	 * @date  2013-07-03 10:10
	 * @return jedisPool
	 */
	public static JedisPool getInstance() {
		if(jedisPool == null ) initialPool();
		return jedisPool;
	}

	public  ShardedJedisPool getShardedJedisPool() {
		if(shardedJedisPool == null ) initialShardedPool();
		return shardedJedisPool;
	}
	
	/**
	 *  初始化数据库连接池
	 * @description: 初始化数据库连接池
	 * @author  WuZhiliang
	 * @date  2013-07-03 10:10
	 */
	private static void initialPool(){
	/*	JedisPoolConfig config = new JedisPoolConfig();
		config.setMaxActive(Integer.valueOf(PropertyUtil.getProInfo(CONFIG,MAXACTIVE)));
		config.setMaxIdle(Integer.valueOf(PropertyUtil.getProInfo(CONFIG,MAXIDLE)));
		config.setMaxWait(Long.valueOf(PropertyUtil.getProInfo(CONFIG,MAXWAIT)));
		config.setTestOnBorrow(Boolean.valueOf(PropertyUtil.getProInfo(CONFIG,TESTONBORROW)));
		config.setTestOnReturn(Boolean.valueOf(PropertyUtil.getProInfo(CONFIG,TESTONRETURN)));
		
		jedisPool = new JedisPool(config, PropertyUtil.getProInfo(CONFIG,IP),
				Integer.valueOf(PropertyUtil.getProInfo(CONFIG,PORT)));*/
	
	}

	private void  initialShardedPool(){
		
	/*	ResourceBundle bundle = ResourceBundle.getBundle("redis");
		if (bundle == null) {
			throw new IllegalArgumentException(
					"[redis.properties] is not found!");
		}
		JedisPoolConfig config = new JedisPoolConfig();
		
		config.setMaxActive(Integer.valueOf(PropertyUtil.getProInfo(CONFIG,MAXACTIVE)));
		config.setMaxIdle(Integer.valueOf(PropertyUtil.getProInfo(CONFIG,MAXIDLE)));
		config.setMaxWait(Long.valueOf(PropertyUtil.getProInfo(CONFIG,MAXWAIT)));
		config.setTestOnBorrow(Boolean.valueOf(PropertyUtil.getProInfo(CONFIG,TESTONBORROW)));
		config.setTestOnReturn(Boolean.valueOf(PropertyUtil.getProInfo(CONFIG,TESTONRETURN)));
		
		JedisShardInfo jedisShardInfo1 = new JedisShardInfo(
				bundle.getString("redis1.ip"), Integer.valueOf(bundle
						.getString("redis.port")));
		JedisShardInfo jedisShardInfo2 = new JedisShardInfo(
				bundle.getString("redis2.ip"), Integer.valueOf(bundle
						.getString("redis.port")));
		
		List<JedisShardInfo> list = new LinkedList<JedisShardInfo>();
		list.add(jedisShardInfo1);
		list.add(jedisShardInfo2);
		shardedJedisPool = new ShardedJedisPool(config, list);*/
		
	}
	
	

}
